﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using Ionic.Zip;
using CRMBiz;
using System.Data;
using CRMWeb.Utilities.Context;

namespace CRMWeb.WebServices
{
    /// <summary>
    /// Summary description for DownloadFiles
    /// </summary>
    public class DownloadFiles : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            int app_id = Convert.ToInt32(context.Request.Params["app_id"].ToString());
            string strFilePath = string.Empty;
            string fileType = context.Request.Params["fileType"].ToString();
            string tableName = string.Empty;
            string folder = string.Empty;
            if (fileType == "IDCheckFile")
            {
                tableName = "crm_idcheckhistory";
                folder = "IDCheck";
            }
            else if (fileType == "PayslipFile")
            {
                tableName = "crm_payslipcheckhistory";
                folder = "PaySlipCheck";
            }
            DataSet ds;
            try
            {
                ds = ApplicationSACCHandler.SelectFile(app_id, tableName, fileType);

                if (ds.Tables[0].Rows.Count > 0)
                {
                    if (ds.Tables[0].Rows.Count == 1)
                    {
                        strFilePath = System.Web.HttpContext.Current.Server.MapPath("~/MCVerifyFile/" + folder + "/" + ds.Tables[0].Rows[0]["files"].ToString());
                        FileInfo fi = new FileInfo(strFilePath);
                        if (fi.Exists)
                        {
                            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename=" + fi.Name);
                            HttpContext.Current.Response.AppendHeader("Content-Length", fi.Length.ToString());
                            HttpContext.Current.Response.ContentType = "application";
                            HttpContext.Current.Response.TransmitFile(strFilePath);
                            HttpContext.Current.Response.Flush();

                        }
                        else
                            HttpContext.Current.Response.Write("0");
                    }
                    else
                    {
                        using (ZipFile zip = new ZipFile())
                        {
                            zip.AlternateEncodingUsage = ZipOption.AsNecessary;
                            zip.AddDirectoryByName(fileType + app_id);

                            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                            {
                                strFilePath = System.Web.HttpContext.Current.Server.MapPath("~/MCVerifyFile/" + folder + "/" + ds.Tables[0].Rows[i]["files"].ToString());
                                FileInfo fi = new FileInfo(strFilePath);
                                if (fi.Exists)
                                    zip.AddFile(strFilePath, fileType + app_id);
                                else
                                {
                                    HttpContext.Current.Response.Write("0");
                                    return;
                                }
                            }
                            context.Response.Clear();
                            context.Response.BufferOutput = false;
                            string zipName = app_id + "_" + fileType + ".zip";

                            context.Response.ContentType = "application/zip";
                            context.Response.AddHeader("content-disposition", "attachment; filename=" + zipName);
                            zip.Save(context.Response.OutputStream);
                        }
                    }
                }
                else
                    context.Response.Write("0");
            }
            catch (Exception ex)
            {
                context.Response.Write("0");
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}